package com.tbit.main.service.impl;

import com.tbit.main.constant.AccountConfigExtConstant;
import com.tbit.main.dao.core.AccountConfigExtDao;
import com.tbit.main.pojo.AccountConfigExt;
import com.tbit.main.service.AccountConfigExtService;
import com.tbit.main.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author LMY
 * @create 2021-12-29 15:28
 */
@Service("accountConfigExtService")
public class AccountConfigExtServiceImpl implements AccountConfigExtService {

    @Autowired
    private AccountConfigExtDao accountConfigExtDao;

    @Override
    public List<AccountConfigExt> getAll() {
        return accountConfigExtDao.getAll();
    }

    @Override
    public AccountConfigExt getParamKey(Integer accountId, String paramKey) {
        return accountConfigExtDao.getParamKey(accountId, paramKey);
    }

    @Override
    public void update(AccountConfigExt accountConfigExt) {
        accountConfigExtDao.update(accountConfigExt.getAccountId(), accountConfigExt.getParamKey(), accountConfigExt.getParamValue());
    }

    @Override
    public void insert(AccountConfigExt accountConfigExt) {
        accountConfigExtDao.insert(accountConfigExt.getAccountId(), accountConfigExt.getParamKey(), accountConfigExt.getParamValue(), DateTimeUtil.getNowTime(), accountConfigExt.getRemark());
    }
    /**
     * @description: 查询 头盔锁配置拓展字段
     * @author: mo.shanyong
     * @date: 2025/4/10 20:19
     * @param: accountId 区域id
     * @return: java.util.List<com.tbit.main.pojo.AccountConfigExt>
     */
    @Override
    public List<AccountConfigExt> getHelmetParam(Integer accountId) {
        List<String> paramKeyList = new ArrayList<>(Arrays.asList(AccountConfigExtConstant.OPEN_HELMET,AccountConfigExtConstant.HELMET,
                AccountConfigExtConstant.HELMET_LOST_CHECK,AccountConfigExtConstant.OPEN_STATUSEX));
        return accountConfigExtDao.getByParamKeys(accountId,paramKeyList);
    }

    /**
     * 新增区域参数扩展配置
     *
     * @param accountConfigExts 区域参数扩展配置
     * @return 结果
     */
    @Override
    public int insertAccountConfigExt(List<AccountConfigExt> accountConfigExts) {
        List<AccountConfigExt> adds = new ArrayList<>();
        Integer accountId = accountConfigExts.get(0).getAccountId();
        List<String> paramKeys = accountConfigExtDao.getKeysByAccountId(accountId);
        /**存在修改，不存在新增*/
        for (AccountConfigExt accountConfigExt : accountConfigExts) {
            if (paramKeys.contains(accountConfigExt.getParamKey())) {
                accountConfigExtDao.updateAccountConfigExt(accountConfigExt);
            } else {
                adds.add(accountConfigExt);
            }
        }
        if (!adds.isEmpty()) {
            accountConfigExtDao.addBatch(adds);
        }
        return 1;
    }
}
